import list from '@/assets/js/menuList.js'
import * as defaultConfig from '@/assets/js/defaultConfig.js'
import menuUtils from '@/utils/menuUtils.js'

// 存放组件之间共享的数据
const state = {
  hideLeft: null,
  hideClass: null,
  leftWidth: defaultConfig.DEFALUT_LEFT_WIDTH,
  menuList: null,
  currentMenuItem: null,
  tagsViewList: [],
  breadcrumbList: []
}

// 类似
const actions = {}

// 获取数据
const getters = {
  getHideLeft: state => {
    if (state.hideLeft === null) {
      state.hideLeft = defaultConfig.DEFAULT_HIDE_LEFT
    }
    return state.hideLeft
  },
  getHideClass: state => {
    if (state.hideClass === null) {
      state.hideClass = defaultConfig.DEFAULT_HIDE_CLASS
    }
    return state.hideClass
  },
  getMenuList: state => {
    if (state.menuList === null) {
      state.menuList = list.menuList
    }
    return state.menuList
  },
  getCurrentMenuItem: state => {
    if (state.currentMenuItem === null) {
      state.currentMenuItem = list.menuList[0]
    }
    return state.currentMenuItem
  },
  getTagsViewList: state => {
    if (state.tagsViewList.length === 0) {
      state.tagsViewList[0] = list.menuList[0]
    }
    return state.tagsViewList
  },
  getBreadcrumbList: state => {
    if (state.breadcrumbList.length === 0) {
      state.breadcrumbList[0] = list.menuList[0]
    }
    return state.breadcrumbList
  }
}

// 显式的更改state里的数据 只能处理同步函数
const mutations = {
  setHideLeft: (state, hideLeft) => {
    state.hideLeft = hideLeft
  },
  setHideClass: (state, hideClass) => {
    state.hideClass = hideClass
  },
  setMenuList: (state, menuList) => {
    state.menuList = menuList
  },
  setCurrentMenuItem: (state, currentMenuItem) => {
    state.currentMenuItem = currentMenuItem
  },
  setBreadcrumbList: (state, item) => {
    const breadcrumbList = menuUtils.getCurrentBreadcrumbList(
      list.menuList,
      item.id
    )
    state.breadcrumbList = breadcrumbList
  },
  addTagsViewItem: (state, item) => {
    menuUtils.addTagsViewItem(state.tagsViewList, item)
  },
  removeTagsViewItem: (state, item) => {
    const cur = menuUtils.removeTagsViewItem(
      state.tagsViewList,
      state.currentMenuItem,
      item
    )

    if (cur !== null) {
      state.currentMenuItem = cur
      state.breadcrumbList = menuUtils.getCurrentBreadcrumbList(
        list.menuList,
        cur.id
      )
    }
  }
}

export default {
  namespaced: true,
  state,
  actions,
  getters,
  mutations
}
